// pages/institute/course/details.js
import {
  getUserCourse,
  getTouristCourse
} from '../../../api/course'
import {
  $wuxDialog
} from '../../../miniprogram_npm/wux-weapp/index';
Page({

      /**
       * 页面的初始数据
       */
      data: {
        index: 0,
        tagStyle: {
          p: 'line-height:24px;font-size:16px;color:#666666;letter-spacing:1px;margin-bottom:15rpx;',
          img: 'width:100vw;height:auto;',
        },
        id: '',
        infoData: {},
        isStaff: false,

        isLogin: false,
        isIOS: false,
        isVip: false,
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {

        this.data.id = Number(options.id);
        this.data.isCanApply = options.isCanApply === '1'
        this.data.isStaff = getApp().globalData.userInfo.companyId === this.data.id;

        this.setData({
          isLogin: getApp().globalData.isLogin,
          isIOS: getApp().globalData.isIOS,
          isVip: getApp().globalData.userInfo.vip,
        })
        this.data.id = options.id
        this.loadData()
      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {

      },
      async loadData() {
        let res;
        if (this.data.isLogin) {
          res = await getUserCourse(this.data.id);
        } else {
          res = await getTouristCourse(this.data.id);
        }
        this.setData({
          infoData: res
        })
      },

      /**
       * 跳转视频播放
       * @param {object} e 
       */
      navigatorPlayVideo(e) {
        const {
          id,
          test,
          supportlistening
        } = e.currentTarget.dataset;
        //不可以申请加入商学院并且是会员(说明已经加入了商学院) 或 是本公司员工
        if ((!this.data.isCanApply && this.data.isVip) || this.data.isStaff || supportlistening) {
          wx.navigateTo({
            url: `/pages/learn/video/video?courseId=${this.data.id}&hourId=${id}`,
          })
        } else {
          if (this.data.isIOS) {
            $wuxDialog().alert({
              resetOnClose: true,
              title: '提示',
              content: 'iPhone用户请联系客服提供学习服务',
              onConfirm(e) {

              },
            })
          } else {
            $wuxDialog().alert({
              resetOnClose: true,
              title: '提示',
              content: '请加入该商学院后进行学习',
              onConfirm(e) {

              },
            })
          } 
        }
      },

      jumpToRecord() {
        wx.navigateTo({
          url: '/pages/learn/record/record?id=' + this.data.id,
        })
      },
      jumpToCompany(e) {
        const {
          id
        } = e.currentTarget.dataset
        wx.navigateTo({
          url: '/pages/institute/details/details?id=' + id,
        })
      },

      jumpToCourse(e) {
            wx.navigateTo({
              url: '/pages/institute/course/details?id=' + e.detail + '&isCanApply='+this.data.isCanApply?'1':'0',
            })
          },
          /**
           * tabs 切换
           */
          handleTabsChange(e) {
            const {
              key
            } = e.detail;
            const index = parseInt(key);
            this.setData({
              key,
              index,
            })
          },

          /**
           * swiper 切换
           */
          handleSwiperChange(e) {
            const {
              current
            } = e.detail
            const key = current.toString();
            this.setData({
              key,
              index: current,
            })
          },
          /**
           * 判断是否登录
           */
          isLogin() {
            return new Promise(resolve => {
              if (this.data.isLogin) {
                resolve()
              } else {
                $wuxDialog().confirm({
                  title: '你还没有登陆哦~',
                  content: '请先登陆再查看更多内容',
                  buttons: [{
                    text: '暂不登录',
                  }, {
                    text: '立即登录',
                    type: 'primary',
                    onTap(e) {
                      wx.navigateTo({
                        url: '/pages/login/accredit/accredit',
                      })
                    }
                  }],
                })
              }
            })
          },

          /**
           * 生命周期函数--监听页面隐藏
           */
          onHide: function () {

          },

          /**
           * 生命周期函数--监听页面卸载
           */
          onUnload: function () {

          },

          /**
           * 页面相关事件处理函数--监听用户下拉动作
           */
          onPullDownRefresh: function () {

          },

          /**
           * 页面上拉触底事件的处理函数
           */
          onReachBottom: function () {

          },

          /**
           * 用户点击右上角分享
           */
          onShareAppMessage: function () {

          }
      })